/**
 * 日志控件
 * 当系统为开发模式，则打印console
 * 当系统为线上模式，则向后台发送打印消息
 * 向后台发送打印消息的逻辑如下：
 * 1. 定时器logMaxWaitTime 毫秒向后台发送打印消息
 * 2. 若在logMaxWaitTime 毫秒内，消息个数超过10K， 向后台发送打印消息
 * 
 * @author hushaoli
 */

const { aliLog } = require("../../api/api");
const { getStorageSync } = require("../../utils/xcxApiPlugin");

 /**
  * 日志缓存
  */
 var logList = "";

 /**
  * 页面运行环境
  * 不同页面拥有不同的运行环境
  * 若是开发环境，则打印在console命令台
  * 若是线上环境，则加入日志队列，等待向后台发送日志消息
  */
var page_env = new Map();

/**
 * 全局日志的页面名称
 */
const global_page_name = "global";

/**
 * 默认全局运行环境为线上模式
 */
page_env.set(global_page_name, "pro");

/**
 * 日志最大存储长度f -- 10k
 */
const logMaxSize = 10 * 1024;
/**
 * 日志最大等待时间 -- 2s
 */
const logMaxWaitTime = 2000;

/**
 * 
 */

 /**
  * 定时任务
  */
 var timer = setTimeout(()=>{
    sendMsgToServer();
 }, logMaxWaitTime);


 function dev(page) {
    if (typeof page == undefined || page == undefined) {
        //未定义page信息，认为是设置全局日志权限
        page = global_page_name;
    }
    page_env.set(page, "dev");
    return this;
 }

 function pro(page) {
    if (typeof page == undefined || page == undefined) {
        //未定义page信息，认为是设置全局日志权限
        page = global_page_name;
    }
    page_env.set(page, "pro");
    return this;
 }

 function log(msg, page) {
    if (typeof page == undefined || page == undefined) {
        //未定义page信息，认为是设置全局日志权限
        page = global_page_name;
    }
    
    var env = page_env.get(page) == undefined ? page_env.get(global_page_name) : page_env.get(page);
    if (env == "dev") {
        //开发模式下，直接打印日志
        console.log("[" + page + "][" + getTimeWithFormate() + "]" + JSON.stringify(msg));
    } else {
        var logMsg = "[" + page + "][" + getTimeWithFormate() + "]" + JSON.stringify(msg) + "\n";
        logList += logMsg;
        if (logList.length > logMaxSize) {
            //调用后台记录日志的接口
            sendMsgToServer();
        }
    }
    return this;
 }

function test(msg, page) {
    if (typeof page == undefined || page == undefined) {
        //未定义page信息，认为是设置全局日志权限
        page = global_page_name;
    }
    var env = page_env.get(page) == undefined ? page_env.get(global_page_name) : page_env.get(page);
    if (env == "dev") {
        //开发模式下，直接打印日志
        console.log("[" + page + "][" + getTimeWithFormate() + "]" + JSON.stringify(msg));
    } else {
       //do nothing
    }
    return this;
}

 function getTimeWithFormate() {
    const nowTime = new Date();
    return nowTime.getFullYear() + "-" + (nowTime.getMonth() + 1) + "-" + nowTime.getDate() + " " + nowTime.getHours() + ":" + nowTime.getMinutes() + ":" + nowTime.getSeconds() + "." + nowTime.getMilliseconds();
 }

 function sendMsgToServer() {
    //重置定时器
    clearTimeout(timer);
    
    if (logList.length > 0) {
        const user = JSON.parse(getStorageSync("user"));
        if (typeof user != undefined && user != undefined && user != null) {
            //发送消息
            aliLog({
                tel : user.tel,
                token : user.token,
                content : logList
            });
            //清空日志缓存
            logList = "";
        }
    }

    timer = setTimeout(() => {
        sendMsgToServer();
    }, logMaxWaitTime);
 }


 export default {
    dev,
    pro,
    log,
    test
 }